package com.itqn.dao.Impl;

import cn.hutool.core.util.StrUtil;
import com.itqn.dao.CategoryDao;
import com.itqn.pojo.Category;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*
 * 分类表Dao层的实现类
 * */
public class CategoryDaoImpl implements CategoryDao {
    private ResultSet rs;

    @Override
    // 查询
    public List<Category> query(String name, Integer pageSize, Integer page) throws Exception {
        List<Category> list = new ArrayList<>();
        Category category = null;
        BaseDao baseDao = new BaseDao();
        StringBuilder sb = new StringBuilder();
        sb.append("select id,`name`,`status` from category ");
        Boolean flag = false;
        if (StrUtil.isNotBlank(name)) {
            flag = true;
            sb.append("where name LIKE CONCAT('%',?,'%') ");
        }
        Boolean isPage = page != null && pageSize != null;
        if (isPage) {
            sb.append("limit ?, ?");
        }

        if (flag) {
            rs = baseDao.getlist(sb.toString(), new Object[]{name, (page - 1) * pageSize, pageSize});
        } else {
            rs = baseDao.getlist(sb.toString(), new Object[]{(page - 1) * pageSize, pageSize});
        }
        if (rs == null) {
            return Collections.emptyList();
        }

        while (rs.next()) {
            category = new Category();
            category.setId(rs.getLong("id"));
            category.setName(rs.getString("name"));
            category.setStatus(rs.getInt("status"));
            list.add(category);
        }
        return list;
    }

    @Override
    public Integer totalCount(String name, Integer pageSize, Integer page) throws Exception {
        BaseDao baseDao = new BaseDao();
        //编写sql语句
        StringBuilder sb = new StringBuilder();
        sb.append("select count(id) from category ");
        if (StrUtil.isNotBlank(name)) {
            sb.append("where name LIKE CONCAT('%',?,'%') ");
        }
        if (StrUtil.isNotBlank(name)) {
            rs = baseDao.getlist(sb.toString(), new Object[]{name});
        } else {
            rs = baseDao.getlist(sb.toString(), new Object[]{});
        }

        int number = 0;
        if (rs.next()) {
            number = rs.getInt(1);
        }
        // number是总条数
        return number;
    }

    @Override
    // 查询
    public List<Category> findAllCategory() throws Exception {
        List<Category> list = new ArrayList<>();
        Category category = null;
        BaseDao baseDao = new BaseDao();
        String sql = "select id,`name` from category where status = 1";
        rs = baseDao.getlist(sql, new Object[]{});
        while (rs.next()) {
            category = new Category();
            category.setId(rs.getLong("id"));
            category.setName(rs.getString("name"));
            list.add(category);
        }
        return list;
    }

    @Override
    //删除
    public int deleteCategory(int id) throws Exception {
        int result = 0;
        BaseDao baseDao = new BaseDao();
        String sql = "DELETE FROM category WHERE id=?";
        result = baseDao.update(sql, new Object[]{id});
        return result;
    }

    @Override
    //添加
    public int AddCategory(Category category) throws Exception {
        if (category.getName().equals("") || category.getName() == null) {
            return 0;
        }
        int result = 0;
        BaseDao baseDao = new BaseDao();
        String sql = "INSERT into category (`name`) value (?)";
        result = baseDao.update(sql, new Object[]{category.getName()});
        return result;
    }

    @Override
    //单查
    public Category findCategoryById(int id) throws Exception {
        BaseDao baseDao = new BaseDao();
        Category category = null;
        String sql = "select id,`name` from category where id=?";
        rs = baseDao.getlist(sql, new Object[]{id});
        while (rs.next()) {
            category = new Category();
            category.setId(rs.getLong("id"));
            category.setName(rs.getString("name"));
        }
        return category;
    }

    @Override
    //修改
    public int updateCategory(Category category) throws Exception {
        int result = 0;
        BaseDao baseDao = new BaseDao();
        String sql = "UPDATE category set `name`=? where id=?";
        result = baseDao.update(sql, new Object[]{category.getName(), category.getId()});
        return result;
    }


    @Override
    public Integer updatestatic(Category category) throws Exception {
        BaseDao baseDao = new BaseDao();
        String sql = "UPDATE category set `status`=? where id=?";
        baseDao.update(sql, new Object[]{category.getStatus(), category.getId()});
        if (category.getStatus() == 0) {
            return 1;
        } else {
            return 0;
        }
    }
}
